package com.hello.pet.config.security;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.util.Assert;

import java.util.Collection;

/**
 * 自定义令牌对象
 */
public class WxAuthenticatioToken extends AbstractAuthenticationToken {
	private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
	private final Object wxLoginParam;
	private String token;

	public WxAuthenticatioToken(String token) {
		super(null);
		this.token = token;
		this.wxLoginParam = null;
		this.setAuthenticated(false);
	}

	public WxAuthenticatioToken(Object wxLoginParam, Collection<? extends GrantedAuthority> authorities) {
		super(authorities);
		this.wxLoginParam = wxLoginParam;
		super.setAuthenticated(true);
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	@Override
	public Object getCredentials() {
		return null;
	}

	@Override
	public Object getPrincipal() {
		return this.wxLoginParam;
	}

	@Override
	public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
		Assert.isTrue(!isAuthenticated, "Cannot set this openId to trusted - use constructor which takes a GrantedAuthority list instead");
		super.setAuthenticated(false);
	}

	@Override
	public void eraseCredentials() {
		super.eraseCredentials();
	}
}
